package cn.cf.kxx.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import cn.cf.kxx.R;
import cn.cf.kxx.fram.OnValveClickListener;
import cn.cf.kxx.model.Valve;
import cn.cf.kxx.tools.Tools;

/**
 * 阀门列表
 */
public class GridValveAdapter extends BaseAdapter {
    private Context mContext;
    private List<Valve> mDataList;

    private OnValveClickListener onValveClickListener;


    public void setOnValveClickListener(OnValveClickListener onValveClickListener) {
        this.onValveClickListener = onValveClickListener;
    }

    public GridValveAdapter(Context context) {
        mContext = context;
    }

    public List<Valve> getmDataList() {
        return mDataList;
    }

    public void setmDataList(List<Valve> mDataList) {
        this.mDataList = mDataList;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.valve_item, parent, false);
            holder = new ViewHolder();
            holder.name = convertView.findViewById(R.id.name);
            holder.temper = convertView.findViewById(R.id.temper);
            holder.humidity = convertView.findViewById(R.id.humidity);
            holder.valveAsw = convertView.findViewById(R.id.valveAsw);
            holder.valveBsw = convertView.findViewById(R.id.valveBsw);
            holder.valveAswS = convertView.findViewById(R.id.valveAswS);
            holder.valveBswS = convertView.findViewById(R.id.valveBswS);
            holder.extVoltage1 = convertView.findViewById(R.id.extVoltage1);
            holder.extVoltage2 = convertView.findViewById(R.id.extVoltage2);
            holder.pulseA = convertView.findViewById(R.id.pulseA);
            holder.pulseB = convertView.findViewById(R.id.pulseB);
            holder.swAopen = convertView.findViewById(R.id.swAopen);
            holder.swAclose = convertView.findViewById(R.id.swAclose);
            holder.swBopen = convertView.findViewById(R.id.swBopen);
            holder.swAopen = convertView.findViewById(R.id.swAopen);
            holder.swBclose = convertView.findViewById(R.id.swBclose);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Valve item = mDataList.get(position);
        Log.e("dong", Tools.gson.toJson(item));
        holder.name.setText(item.getName()+"号阀门");
        holder.temper.setText("温度："+ item.getTemper() +"度");
        holder.humidity.setText("湿度："+ item.getHumidity() +"%");
        //0关 1开 2过水  3禁用  4跑水
        holder.valveAsw.setText(getStatus(item.getValveA()));
        holder.valveBsw.setText(getStatus(item.getValveB()));

        holder.valveAswS.setText(getXl(item.getValveA()));
        holder.valveBswS.setText(getXl(item.getValveB()));

        holder.extVoltage1.setText(item.getExtVoltage1() +"Mpa");
        holder.extVoltage2.setText(item.getExtVoltage1() +"Mpa");

        holder.pulseA.setText(item.getPulseA() +"min/L");
        holder.pulseB.setText(item.getPulseB() +"min/L");

        holder.swAopen.setOnClickListener(v -> {
            onValveClickListener.click(item,"AOPEN");
        });
        holder.swAclose.setOnClickListener(v -> {
            onValveClickListener.click(item,"ACLOSE");
        });

        holder.swBopen.setOnClickListener(v -> {
            onValveClickListener.click(item,"BOPEN");
        });
        holder.swBclose.setOnClickListener(v -> {
            onValveClickListener.click(item,"BCLOSE");
        });

        return convertView;
    }

    private String getStatus(String sw){
        String result = "";
        switch (Integer.parseInt(sw)){
            case 0:
                result = "已关闭";
                break;
            case 1:
                result = "已打开";
                break;
            case 2:
                result = "已过水";
                break;
            case 3:
                result = "已禁用";
                break;
            case 4:
                result = "泄露";
                break;
        }
        return result;
    }

    private String getXl(String sw){
        String result = "";
        switch (Integer.parseInt(sw)){
            case 4:
                result = "泄露";
                break;
            default:
                result = "正常";
        }
        return result;
    }

    static class ViewHolder {
        TextView name;
        TextView temper;
        TextView humidity;
        TextView valveAsw;//
        TextView valveBsw;//
        TextView valveAswS;//跑水
        TextView valveBswS;//跑水
        TextView extVoltage1;
        TextView extVoltage2;
        TextView pulseA;
        TextView pulseB;
        Button swAopen;
        Button swAclose;
        Button swBopen;
        Button swBclose;
    }
}
